﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LastoRevival.Service.Contracts;
using LastoRevival.Website.Models;
using LastoRevival.Website.Data;
using LastoRevival.Service;
using LastoRevival.Service.Contracts.Models;
using LastoRevival.Service.Implementations.Exceptions;
using LastoRevival.Service.Implementations;

namespace LastoRevival.Website.Controllers
{

	public class LastoControllerBase : Controller
	{		
		//protected delegate ActionResult RequestDelegate();

		// singleton
		private static ILastoRevivalService serviceInstance;
		private static IDataProvider siteDataProvider;
		private static object threadLock = new object();
		

		protected override void OnActionExecuted(ActionExecutedContext filterContext)
		{
			// add authentication state
			ViewBag.IsAuthenticated = Service.IsAuthenticated(LoginState.Token);
			ViewBag.UserSettings = UserSettings;
			ViewBag.UserProfile = UserProfile;
			
			base.OnActionExecuted(filterContext);
		}

		protected override void OnException(ExceptionContext filterContext)
		{
			// handle certain exceptions specially
			var type = filterContext.Exception.GetType();

			if (type == typeof(NoAccessException))
			{
				filterContext.Result = AccessError("No access");
				filterContext.ExceptionHandled = true;
			}

			if (type == typeof(SessionExpiredException))
			{
				filterContext.Result = RedirectToAction("Login", "User", new { returnUrl = HttpContext.Request.Url.PathAndQuery });
				filterContext.ExceptionHandled = true;
			}


			base.OnException(filterContext);
		}
		

		protected ViewResult AccessError(string message)
		{
			ViewBag.Error = message;
			return View("NoAccess");
		}

		protected ViewResult ForumError(string message)
		{
			ViewBag.Error = message;
			return View("ForumError");
		}

		protected ViewResult ForumMessage(string message)
		{
			ViewBag.Message = message;
			return View("ForumMessage");
		}

		//protected ActionResult AuthenticatedRequest(RequestDelegate del)
		//{
		//    try
		//    {
		//        return del.Invoke();
		//    }
		//    catch (SessionExpiredException)
		//    {
		//        return RedirectToAction("Login", "User", new { returnUrl = HttpContext.Request.Url.PathAndQuery });
		//    }
		//}
						

		protected ILastoRevivalService Service
		{
			get
			{
				if (serviceInstance == null)
				{
					lock (threadLock)
					{
						if (serviceInstance == null)
						{
							serviceInstance = new LastoRevivalService();
						}
					}
				}

				return serviceInstance;
			}
		}

		protected IDataProvider SiteDataProvider
		{
			get
			{
				if (siteDataProvider == null)
				{
					lock (threadLock)
					{
						if (siteDataProvider == null)
						{
							siteDataProvider = DataProviderFactory.GetDataProvider();
						}
					}
				}

				return siteDataProvider;
			}
		}

		protected SiteUserSettings UserSettings
		{
			get
			{
				return DataProviderFactory.GetDataProvider().GetCurrentUserSettings(UserProfile.Details.UserID);
			}
		}

		protected UserProfile UserProfile
		{
			get 
			{
				return Service.GetActiveProfile(LoginState.Token); 
			}
		}

	}
}